//
//  CustomTableViewCell.swift
//  ERecycle-Shop
//
//  Created by abb on 16/8/18.
//  Copyright © 2016年 abb. All rights reserved.
//

import UIKit

protocol CustomTableViewCellDelegate:NSObjectProtocol {
    func CilckDial()
}

class CustomTableViewCell: UITableViewCell {
    
    var user:User? {
        didSet {
            iconView.sd_setImageWithURL(user?.userImageUrl, placeholderImage: UIImage(named: "user_default"))
            nameLabel.text = user?.nickName
        }
    }
    
    var phoneNumber:String?
    
    weak var delegate:CustomTableViewCellDelegate?
    
    func dial() {
        delegate?.CilckDial()
//        if phoneNumber != nil && phoneNumber != "" {
//            let alertController = UIAlertController(title: "拨打 \(phoneNumber)?", message: "提示", preferredStyle: UIAlertControllerStyle.Alert)
//            let actionDone = UIAlertAction(title: "好", style: .Destructive, handler: { (action) in
//                let phoneStr = "tel:\(self.phoneNumber!)"
//                UIApplication.sharedApplication().openURL(NSURL(string: phoneStr)!)
//            })
//            let actionCancel = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
//            alertController.addAction(actionDone)
//            alertController.addAction(actionCancel)
//            self.pres
//        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    private func setupUI() {
        // 添加子控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(telBtn)
        // 布局子控件
        iconView.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 75, height: 75))
            make.centerY.equalTo(self)
            make.left.equalTo(20)
            
        }
        nameLabel.snp_makeConstraints { (make) in
            make.left.equalTo(iconView.snp_right).offset(20)
            make.centerY.equalTo(iconView.snp_centerY)
        }
        telBtn.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 50, height: 50))
            make.centerY.equalTo(self)
            make.right.equalTo(-20)
        }
        // 添加监听
        telBtn.addTarget(self, action: #selector(dial), forControlEvents: .TouchUpInside)
    }
    
    // MARK: - 懒加载
    
    private lazy var iconView:UIImageView = {
        let iv = UIImageView()
        iv.layer.cornerRadius = 37.5
        iv.layer.masksToBounds = true
        iv.image = UIImage(named: "user_default")
        return iv
    }()
    private lazy var nameLabel:UILabel = UILabel.creatLabel(color: UIColor.blackColor(), fontSize: 24)
    
    private lazy var telBtn:UIButton = {
        let btn = UIButton()
        btn.layer.cornerRadius = 25
        btn.layer.borderWidth = 0.5
        btn.setImage(UIImage(named: "call"), forState: .Normal)
        return btn
    }()    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
